package com.cropflow.controller;

import com.cropflow.entity.Field;
import com.cropflow.service.FieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/fields")
public class FieldController {
    
    @Autowired
    private FieldService fieldService;
    
    @GetMapping
    public String listFields(Model model) {
        List<Field> fields = fieldService.getAllFields();
        model.addAttribute("fields", fields);
        return "fields/list";
    }
    
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("field", new Field());
        return "fields/add";
    }
    
    @PostMapping("/add")
    public String addField(@ModelAttribute Field field) {
        fieldService.saveField(field);
        return "redirect:/fields";
    }
    
    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable Long id, Model model) {
        Field field = fieldService.getFieldById(id);
        model.addAttribute("field", field);
        return "fields/edit";
    }
    
    @GetMapping("/map/{id}")
    public String showFieldMap(@PathVariable Long id, Model model) {
        Field field = fieldService.getFieldById(id);
        model.addAttribute("field", field);
        return "fields/map";
    }
}